import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;

public class UDPClient extends Thread {

	public static final int NumberOfClients = 1;
	public static final int NumberOfMessages = 3;
	
	private String serverIP;
	private int serverPort;
	public int clientPort;
	
	public UDPClient(String serverIP, int serverPort, int clientPort) {
		this.serverIP = serverIP;
		this.serverPort = serverPort;
		this.clientPort = clientPort;
	}
	
	@Override
	public void run()
	{
		try {
			DatagramSocket socket = new DatagramSocket(this.clientPort);

			byte[] msg = ("Sent from Client").getBytes();
			InetAddress serverAddr = InetAddress.getByName(this.serverIP);
			DatagramPacket packet = new DatagramPacket(msg, 0, msg.length, serverAddr, this.serverPort);
			socket.send(packet);
				
			byte[] recvBuf = new byte[1024];
			DatagramPacket ansPacket = new DatagramPacket(recvBuf, 0, recvBuf.length);
			socket.receive(ansPacket);
				
			System.out.println("Received from Server" + new String(ansPacket.getData(), 0, ansPacket.getLength()));
	
			// Wait a bit before sending the next message.
			Thread.sleep(100);
						
		} catch (Exception e) {
			
			System.err.println("Client Exception: " + e);
			e.printStackTrace();
		}
	}
}
